package com.csun.cmny.provider.web.rpc;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.provider.dto.DeploymentDto;
import com.csun.cmny.provider.model.constant.ReviseConstant;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcActivitiFeignApi;
import com.csun.cmny.provider.service.OmcActivitiService;
import com.csun.cmny.provider.service.OmcReviseVarService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.activiti.engine.repository.Deployment;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-12-12 19:44
 * @Description:
 */
@RestController
public class OmcActivitiFeignClient implements OmcActivitiFeignApi {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcReviseVarService omcReviseVarService;
    @Resource
    private OmcActivitiService omcActivitiService;

    @Override
    public Wrapper<Integer> reviseProcessDeploymentCreate(@RequestParam("groupId") Long groupId) {

        LoginAuthDto loginAuthDto = new LoginAuthDto();
        loginAuthDto.setGroupId(groupId);
        loginAuthDto.setUserId(1l);
        loginAuthDto.setUserName("admin");

        DeploymentDto deploymentDto = new DeploymentDto();
        deploymentDto.setProcessKey(ReviseConstant.PROCESS_KEY_REVISE);
        deploymentDto.setDeploymentName("调房调级流程");
        deploymentDto.setDeploymentResource("activiti/ReviseProcess.bpmn");

        Deployment deployment = omcActivitiService.createDeployment(deploymentDto, loginAuthDto);
        if (PublicUtil.isEmpty(deployment)) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        // 批量插入调房调级变量
        omcReviseVarService.init(loginAuthDto);

        return WrapMapper.ok();
    }
}
